/**
 * 
 */
package br.com.tiexpress.util;

import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

import org.apache.log4j.Level;
import org.apache.log4j.Logger;

/**
 * @author yawit
 *
 */
public class Connection {
	public static Logger log = Logger.getLogger(Connection.class
			.toString());
	private static final String DRIVER = "org.postgresql.Driver";
	private static final String URL = "jdbc:postgres://localhost:5432/ilunch";
	private static final String USERNAME = "postgres";
	private static final String PASSWORD = "postgre";

	public static Connection getConnection()
			throws ClassNotFoundException, SQLException {
		Class.forName(DRIVER);
		log.log(Level.INFO, "Buscando Conexao no PostgreSQL");

		java.sql.Connection c = DriverManager.getConnection(URL, USERNAME, PASSWORD);
		
		c.setAutoCommit(false);
		return (Connection) c;
	}
	
	public static void closeAll(Connection c){
		if(c!=null)
			try {
				((Statement) c).close();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
	}
	
	public static void closeAll(Connection c, PreparedStatement stm){
		closeAll(c);
		closeAll(stm);
	}
	
	public static void closeAll(Connection c, PreparedStatement stm,ResultSet rs){
		closeAll(c);
		closeAll(stm);
		closeAll(rs);
	}
	
	public static void closeAll(PreparedStatement stm){
		if(stm!=null)
			try {
				stm.close();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
	}
	
	
	public static void closeAll(PreparedStatement stm,ResultSet rs){
		closeAll(stm);
		closeAll(rs);
	}
	
	
	public static void closeAll(ResultSet rs){
		if(rs!=null)
			try {
				rs.close();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
	}
	
	
	public static void closeAll(Connection c,ResultSet rs){
		closeAll(c);
		closeAll(rs);
	}
}
